<template>
  <div class="app-container">

    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">

        <el-form-item label="类型名称" prop="projectTypeName">
          <el-input
            v-model="queryParams.projectTypeName"
            placeholder="请输入项目类型名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="border-radius: 10px">
      <el-table
        border
        v-loading="loading"
        :data="projectTypeList"
        row-key="projectTypeId"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column label="项目类型编码" align="center" prop="projectTypeCode" />
        <el-table-column label="项目类型名称" align="center" prop="projectTypeName" />
<!--        <el-table-column label="是否启用" align="center" prop="enableFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enableFlag"/>
          </template>
        </el-table-column>-->
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['pc:projectType:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleAdd(scope.row)"
              v-hasPermi="['pc:projectType:add']"
            >新增</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['pc:projectType:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>


    <!-- 添加或修改项目分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="项目类型编码" prop="projectTypeCode">
              <el-input
                v-model="form.projectTypeCode"
                :readonly="form.projectTypeId"
                placeholder="不填写时将自动生成编码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24" v-if="form.parentTypeId !== 0">
            <el-form-item label="父类型" prop="parentTypeId">
              <treeselect
                v-model="form.parentTypeId"
                :options="projectTypeOptions"
                :normalizer="normalizer"
                placeholder="请选择父类型" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="项目类型名称" prop="projectTypeName">
              <el-input v-model="form.projectTypeName" placeholder="请输入项目类型名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-form-item label="是否启用" prop="enableFlag">
          <el-input v-model="form.enableFlag" placeholder="请输入是否启用" />
        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProjectType,
  getProjectType,
  delProjectType,
  addProjectType,
  updateProjectType
} from "@/api/mes/pc/projectType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "ProjectType",
  dicts: ['sys_yes_no'],
  components: {
    Treeselect
  },
  data() {
    return {
      optType:undefined,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 项目分类表格数据
      projectTypeList: [],
      // 项目分类树选项
      projectTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectTypeCode: null,
        projectTypeName: null,
        parentTypeId: null,
        ancestors: null,
        enableFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentTypeId: [
          { required: true, message: "父类型不能为空", trigger: "blur" }
        ],
        projectTypeName: [
          { required: true, message: "项目类型名称不能为空", trigger: "blur" }
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目分类列表 */
    getList() {
      this.loading = true;
      listProjectType(this.queryParams).then(response => {
        this.projectTypeList = this.handleTree(response.data, "projectTypeId", "parentTypeId")
        this.loading = false;
      });
    },
    /** 转换项目类型数据结构 **/
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.projectTypeId,
        label: node.projectTypeName,
        children: node.children
      };
    },
    /** 查询项目分类下拉树结构 */
    getTreeSelect() {
      listProjectType().then(response => {
        this.projectTypeOptions = [];
        const data = this.handleTree(response.data, "projectTypeId", "parentTypeId")[0];
        this.projectTypeOptions.push(data);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectTypeId: null,
        projectTypeCode: null,
        projectTypeName: null,
        parentTypeId: 1,
        ancestors: null,
        enableFlag: 'Y',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeSelect();
      if (row != null && row.projectTypeId) {
        this.form.parentTypeId = row.projectTypeId;
      } else {
        this.form.parentTypeId = 0;
      }
      this.open = true;
      this.title = "添加项目分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeSelect();
      if (row != null) {
        this.form.parentTypeId = row.projectTypeId;
      }
      const projectTypeId = row.projectTypeId;
      getProjectType(projectTypeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectTypeId != null) {
            updateProjectType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectTypeIds = row.projectTypeId;
      this.$modal.confirm('是否确认删除项目分类编号为"' + projectTypeIds + '"的数据项？').then(function() {
        return delProjectType(projectTypeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pc/projectType/export', {
        ...this.queryParams
      }, `项目分类_${new Date().getTime()}.xlsx`)
    },


  }
};
</script>
